package com.easy.modules.sysmgr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easy.common.dao.IBaseDao;
import com.easy.common.util.BeanUtils;
import com.easy.common.web.page.DataGrid;
import com.easy.common.web.page.Msg;
import com.easy.common.web.page.Pager;
import com.easy.modules.sysmgr.entity.UserRoleEntity;
import com.easy.modules.sysmgr.service.UserRoleServiceI;
import com.easy.modules.sysmgr.web.form.UserRoleForm;

@Service
@Transactional
public class UserRoleServiceImpl implements UserRoleServiceI {

	@Autowired
	private IBaseDao<UserRoleEntity> basedaoUser;
	
	
	@Override
	public Msg save(UserRoleForm form) {
		try {
			UserRoleEntity entity = new UserRoleEntity();
			BeanUtils.copyNotNullProperties(form, entity);
			entity.setCreated(new Date()) ; 
			
			this.basedaoUser.add(entity);
			
			return new Msg(true, "创建成功！") ;
		} catch (BeansException e) {
			return new Msg(false, "创建失败！") ;
		}
	}

	@Override
	public Msg delete(UserRoleForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					this.basedaoUser.delete(UserRoleEntity.class, id) ;
				}
				return new Msg(true, "删除成功！") ;
			} else {
				return new Msg(false, "删除失败！") ;
			}
		} catch (BeansException e) {
			return new Msg(false, "删除失败！") ;
		}
	}
	
	@Override
	public Msg edit(UserRoleForm form) {
		try {
			UserRoleEntity entity = this.basedaoUser.load(UserRoleEntity.class, form.getId());
			
			if(entity != null) {
				form.setCreated(new Date()) ;
				BeanUtils.copyNotNullProperties(form, entity, new String[] { "created" });
				entity.setModifyDate(new Date()) ;
				
				this.basedaoUser.update(entity) ;
			}
			return new Msg(true, "编辑成功！") ;
		} catch (BeansException e) {
			return new Msg(false, "编辑失败！") ;
		}
	}
	

	@Override
	public UserRoleForm get(UserRoleForm form) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from UserRoleEntity t where 1=1";
		hql = addWhere(hql, form, params) ;
		
		UserRoleEntity entity = (UserRoleEntity) this.basedaoUser.queryObject(hql, params) ;
		if(null != entity) {
			UserRoleForm pform = new UserRoleForm();
			BeanUtils.copyNotNullProperties(entity, pform);
			
			return pform;
		} else {
			return null ;
		}
	}

	@Override
	public DataGrid datagrid(UserRoleForm form) {
		
		List<UserRoleForm> forms = new ArrayList<UserRoleForm>();
		Pager<UserRoleEntity> pager = this.find(form) ;
		if (null != pager && !pager.getDataRows().isEmpty()) {
			for (UserRoleEntity i : pager.getDataRows()) {
				UserRoleForm uf = new UserRoleForm();
				BeanUtils.copyNotNullProperties(i, uf);
				forms.add(uf);
			}
		}
		
		DataGrid datagrid = new DataGrid();
		datagrid.setTotal(pager.getTotal());
		datagrid.setRows(forms);
		return datagrid;
	}


	private Pager<UserRoleEntity> find(UserRoleForm form) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "select t from UserRoleEntity t where 1=1";
		hql = addWhere(hql, form, params) ;
		return this.basedaoUser.find(hql, params);
	}


	private String addWhere(String hql, UserRoleForm form, Map<String, Object> params) {
		if (form.getId() != null && !"".equals(form.getId())) {
			hql += " and t.id=:id";
			params.put("id", form.getId());
		}
		if (form.getName() != null && !"".equals(form.getName())) {
			hql += " and t.name=:name";
			params.put("name", form.getName());
		}
		return hql;
	}
}
